<?php
require_once(dirname(__FILE__).'/../../WildPack/WildPack.php');
$wp = new WildPack();
$wp->using('util');
$wp->using('base');

class TestWpObservable extends WpTestSuite
{
  private $exphd = '';
  private $firedhd = '';
    
  public function hdEv1($str, $int)
  {
    $this->firedhd = 'ev1';
    $this->addResult('Correct handler', 'ev1', $this->exphd, '===');
    $this->addResult('Correct param 1', 'param1', $str, '===');
    $this->addResult('Correct param 2', 2, $int, '===');
  }
  
  public function hdEv2($p1)
  {
    $this->firedhd = 'ev2';
    $this->addResult('Correct handler', 'ev2', $this->exphd, '===');
    $this->addResult('Check param', null, $p1, 'null');
  }
  
  public function hdEv3($p1)
  {
    $this->firedhd = 'ev3';  
    $this->addResult('Correct handler', 'ev3', $this->exphd, '===');
    $this->addResult('Check param', null, $p1, 'null');
  }
  
  public function hdEv4()
  {
    $this->firedhd = 'ev4';
    $this->addResult('Correct handler', 'ev4', $this->exphd, '===');
  }
  
  /**
   * Preparation
   */
  public function prepare(){ return true; }
  
  /**
   * Test
   */
  public function test()
  {
    $o = new TestClass();
    $o->on('ev1', $this, 'hdEv1');
    $o->on('ev2', $this, 'hdEv2');
    $o->on('ev3', $this, 'hdEv3');
    $o->on('ev4', $this, 'hdEv4');
    
    $this->exphd = 'ev1';
    $this->firedhd = '';
    $o->fireEv1();
    $this->addResult('Missed handler?', 'ev1', $this->firedhd, '===');
    
    $this->exphd = 'ev2';
    $this->firedhd = '';
    $o->fireEv2();
    $this->addResult('Missed handler?', 'ev2', $this->firedhd, '===');
    
    $this->exphd = 'ev3';
    $this->firedhd = '';
    $o->fireEv3();
    $this->addResult('Missed handler?', 'ev3', $this->firedhd, '===');
    
    $this->exphd = 'ev4';
    $this->firedhd = '';
    $o->fireEv4();
    $this->addResult('Called non-existing event?', '', $this->firedhd, '===');
  }
  
  /**
   * Cleanup
   */
  public function cleanup(){}
  
  
}

/**
 * A tes class extending the observable class
 */
class TestClass extends WpObservable
{
  public function __construct() { $this->addEvents(array('ev1', 'ev2', 'ev3')); }
  public function fireEv1() { $this->fireEvent('ev1', array('param1', 2)); }
  public function fireEv2() { $this->fireEvent('ev2', array()); }
  public function fireEv3() { $this->fireEvent('ev3', array()); }
  public function fireEv4() { $this->fireEvent('ev4', array()); }
}


$t = new TestWpObservable();
$t->start('WpObservable');
